const { mock, Random } = require('mockjs');

const user = [
    { name: 'admin', pwd: 'admin' },
    { name: 'dev', pwd: 'dev' },
    { name: 'test', pwd: 'test' }
];
const role = [
    { name: 'admin', description: '管理员' },
    { name: 'dev', description: '开发人员' },
    { name: 'test', description: '测试人员' }
];
const user_role = [
    { userName: 'admin', roleName: 'admin' },
    { userName: 'dev', roleName: 'dev' },
    { userName: 'test', roleName: 'test' }
];
const permission = [
    { name: 'add', description: '新增' },
    { name: 'update', description: '修改' },
    { name: 'remove', description: '删除' }
];
const role_route = [
    { roleName: 'admin', id: 1, permission: [] },
    { roleName: 'admin', id: 10, permission: [] },
    { roleName: 'admin', id: 2, permission: [] },
    { roleName: 'admin', id: 20, permission: [] },
    { roleName: 'admin', id: 21, permission: [] },
    { roleName: 'admin', id: 22, permission: [] },
    { roleName: 'admin', id: 3, permission: [] },
    { roleName: 'admin', id: 30, permission: [] },
    { roleName: 'admin', id: 300, permission: [] },
    { roleName: 'admin', id: 31, permission: [] },
    { roleName: 'admin', id: 310, permission: [] },
    { roleName: 'admin', id: 4, permission: [] },
    { roleName: 'admin', id: 40, permission: [] },
    { roleName: 'admin', id: 41, permission: [] },
    { roleName: 'admin', id: 42, permission: [] },
    { roleName: 'admin', id: 43, permission: [] },
    { roleName: 'admin', id: 5, permission: [] },
    { roleName: 'admin', id: 50, permission: ['add', 'update', 'remove'] },

    { roleName: 'dev', id: 1, permission: [] },
    { roleName: 'dev', id: 10, permission: [] },
    { roleName: 'dev', id: 5, permission: [] },
    { roleName: 'dev', id: 50, permission: ['add'] },

    { roleName: 'test', id: 1, permission: [] },
    { roleName: 'test', id: 10, permission: [] },
    { roleName: 'test', id: 5, permission: [] },
    { roleName: 'test', id: 50, permission: ['update'] }
];
const route = [
    {
        id: 2,
        parentId: 0,
        name: 'project',
        path: '/project',
        component: 'Layout',
        redirect: '/project/project-list',
        meta: { title: '项目管理', icon: 'el-icon-phone' }
    },
    {
        id: 20,
        parentId: 2,
        name: 'project-list',
        path: '/project/project-list',
        component: 'project-list',
        meta: { title: '项目列表', icon: 'el-icon-goods' }
    },
    {
        id: 21,
        parentId: 2,
        name: 'project-detail',
        path: '/project/project-detail/:projName',
        component: 'project-detail',
        meta: {
            title: '项目详情',
            icon: 'el-icon-question',
            activeMenu: '/project/project-list',
            hidden: true
        }
    },
    {
        id: 22,
        parentId: 2,
        name: 'project-import',
        path: '/project/project-import',
        component: 'project-import',
        meta: { title: '项目导入', icon: 'el-icon-help' }
    },
    {
        id: 3,
        parentId: 0,
        name: 'nav',
        path: '/nav',
        component: 'Layout',
        redirect: '/nav/second-nav/third-nav',
        meta: { title: '多级导航', icon: 'el-icon-picture' }
    },
    {
        id: 30,
        parentId: 3,
        name: 'second-nav',
        path: '/nav/second-nav',
        redirect: '/nav/second-nav/third-nav',
        component: 'second-nav',
        meta: { title: '二级导航', icon: 'el-icon-camera' }
    },
    {
        id: 300,
        parentId: 30,
        name: 'third-nav',
        path: '/nav/second-nav/third-nav',
        component: 'third-nav',
        meta: { title: '三级导航', icon: 'el-icon-s-platform' }
    },
    {
        id: 31,
        parentId: 3,
        name: 'second-text',
        path: '/nav/SecondText',
        redirect: '/nav/second-text/third-text',
        component: 'second-text',
        meta: { title: '二级文本', icon: 'el-icon-s-opportunity' }
    },
    {
        id: 310,
        parentId: 31,
        name: 'third-text',
        path: '/nav/second-text/third-text',
        component: 'third-text',
        meta: { title: '三级文本', icon: 'el-icon-menu' }
    },
    {
        id: 4,
        parentId: 0,
        name: 'components',
        path: '/components',
        component: 'Layout',
        redirect: '/components/open-window-test',
        meta: { title: '组件测试', icon: 'el-icon-phone' }
    },
    {
        id: 40,
        parentId: 4,
        name: 'open-window-test',
        path: '/components/open-window-test',
        component: 'open-window-test',
        meta: { title: '选择页', icon: 'el-icon-goods' }
    },
    {
        id: 41,
        parentId: 4,
        name: 'card-list-test',
        path: '/components/card-list-test',
        component: 'card-list-test',
        meta: { title: '卡片列表', icon: 'el-icon-question' }
    },
    {
        id: 42,
        parentId: 4,
        name: 'table-search-test',
        path: '/components/table-search-test',
        component: 'table-search-test',
        meta: { title: '表格搜索', icon: 'el-icon-question' }
    },
    {
        id: 43,
        parentId: 4,
        name: 'list-test',
        path: '/components/list-test',
        component: 'list-test',
        meta: { title: '标签页列表', icon: 'el-icon-question' }
    },
    {
        id: 5,
        parentId: 0,
        name: 'permission',
        path: '/permission',
        component: 'Layout',
        redirect: '/permission/directive',
        meta: { title: '权限管理', icon: 'el-icon-phone' }
    },
    {
        id: 50,
        parentId: 5,
        name: 'directive',
        path: '/permission/directive',
        component: 'directive',
        meta: { title: '指令管理', icon: 'el-icon-goods' }
    }
];

const setToken = function (name) {
    return `token_${name}_token`;
};
const checkToken = function (token) {
    return token?.replace(/^token_([\w|\W]+)_token$/, '$1');
};
const login = function (name, pwd) {
    return user.findIndex((v) => v.name === name && v.pwd === pwd) !== -1;
};
const getUser = function (name) {
    return {
        name,
        role: user_role
            .filter((v) => v.userName === name)
            .map((v) => v.roleName)
    };
};
const getRoute = function (name) {
    const { role } = getUser(name);
    const arr = role_route.filter(
        (v) => role.findIndex((val) => val === v.roleName) !== -1
    );
    const filterRoute = [];
    route.forEach((v) => {
        arr.forEach((val) => {
            if (val.id === v.id) {
                const obj = Object.assign({}, v);
                obj.meta.permission = val.permission;
                filterRoute.push(obj);
            }
        });
    });
    return filterRoute;
};

Random.extend({
    tag: function () {
        const tag = ['家', '公司', '学校', '超市'];
        return this.pick(tag);
    }
});
const tableList = mock({
    // 属性 list 的值是一个数组，其中含有 1 到 10 个元素
    'list|30': [
        {
            // 属性 id 是一个自增数，起始值为 1，每次增 1
            'id|+1': 1,
            date: () => Random.date('yyyy-MM-dd'),
            name: () => Random.name(),
            address: () => Random.cparagraph(1),
            tag: () => Random.tag(),
            amt: () => Number(Random.float(-100000, 100000).toFixed(2))
        }
    ]
});

module.exports = [
    {
        url: '/login',
        type: 'post',
        response: (config) => {
            const { username, password } = config.body;
            if (login(username, password)) {
                return {
                    code: 20000,
                    message: '登陆成功',
                    data: setToken(username)
                };
            }
            return {
                code: 401,
                message: '用户名或密码错误',
                data: ''
            };
        }
    },
    {
        url: '/getUser',
        type: 'get',
        response: (config) => {
            const { token } = config.query;
            const userName = checkToken(token);
            if (!userName) {
                return {
                    code: 50008,
                    message: '身份认证失败',
                    data: ''
                };
            }
            return {
                code: 20000,
                message: '',
                data: getUser(userName)
            };
        }
    },
    {
        url: '/getRoute',
        type: 'get',
        response: (config) => {
            const { token } = config.query;
            const userName = checkToken(token);
            if (!userName) {
                return {
                    code: 50008,
                    message: '身份认证失败',
                    data: ''
                };
            }
            return {
                code: 20000,
                data: getRoute(userName),
                message: ''
            };
        }
    },
    {
        url: '/getTableList',
        type: 'get',
        response: (config) => {
            const { page, size, tag } = config.query;
            const data =
                tag === '所有'
                    ? tableList.list
                    : tableList.list.filter((v) => v.tag === tag);
            return {
                code: 20000,
                message: '',
                data: {
                    data: data.filter(
                        (v, i) => i >= (page - 1) * size && i < page * size
                    ),
                    total: data.length
                }
            };
        }
    }
];
